package aden.Generic.Param;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Vector;

public class GenParamNode extends GenParamData
{
	public static final int sNodeBeginChar = '{';
	public static final int sNodeEndChar = '}';
	
	
	public GenParamNode(String inName)
	{
		super(inName,Type_NODE);
		mOutputStreamWriter = null;
	}

	public GenParamNode(String inName,OutputStreamWriter inOutputStreamWriter)
	{
		super(inName,Type_NODE);
		mOutputStreamWriter = inOutputStreamWriter;
		mIsLocked = false;
		
		// on ecrit le character de debut du Node
		try {
			mOutputStreamWriter.write(sNodeBeginChar);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean Export(OutputStreamWriter inOutputStreamWriter)
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		assert false;
		// ## a faire
		return true;
	}

	public boolean Import(InputStream inInputStream)
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		assert false;
		// ## a faire
		return true;
	}

	public boolean Add(GenParamData inData,
			           int          inIndex)
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		mDatas.add(inIndex,inData);
		return true;
	}
	public boolean Add(GenParamData inData)
	{
		if (mOutputStreamWriter != null)
		{
			if (mIsLocked)
			{
				assert false;
				return false;
			}
			try {
				// on ecrit dans le fichier mOutputStreamWriter.
				switch (inData.GetType())
				{
				case Type_NODE:
					// on ecrit le character de fin du Node, le reste ayant deja ete ecrit
					mOutputStreamWriter.write(sNodeEndChar);
					((GenParamNode)inData).Lock();
					break;
				case Type_INT:
					mOutputStreamWriter.write(inData.GetIntValue().GetValue());					
					break;
				case Type_DOUBLE:
					mOutputStreamWriter.write(Double.toString(inData.GetDoubleValue().GetValue()));					
					break;
				case Type_STRING:
					mOutputStreamWriter.write(inData.GetStringValue().GetValue());					
					break;
				default:
					assert(false);
					return false;
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		else
		{
			mDatas.add(inData);
		}
		return true;
	}

	public boolean Remove(int inIndex)
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		mDatas.remove(inIndex);
		return true;
	}
	
	public int GetSize()
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		return mDatas.size();
	}
	
	public GenParamData Get(int inIndex)
	{
		if (mOutputStreamWriter != null)
		{
			assert false;
		}
		return mDatas.get(inIndex);
	}
	
	public OutputStreamWriter GetOutputStreamWriter() {return mOutputStreamWriter;}
	
	private Vector<GenParamData> mDatas;

	private boolean Lock()
	{
		mIsLocked = true;
		return true;
	}
	
	OutputStreamWriter mOutputStreamWriter;
	boolean mIsLocked; // use to insure in outputstream mode, we build the node correctly
}

